﻿<?php


function login($usr, $pwd){
	$response=new WsResponse();	
	if (session_login($usr,$pwd)){
		$response->setStatusCode("0");
		$response->setStatusMessage("Session established");		
	}else{
		$response->setStatusCode("-1");
		$response->setStatusMessage("Bad login");				
	}
	echo($response->toXml());
}

function logout(){
	session_logout();
	$response=new WsResponse();	
	$response->setStatusCode("0");
	$response->setStatusMessage("Session logged out");
	echo($response->toXml());
}
//////////////////////////////////////// Composants /////////////////////////////////////
function getComposants(){
	$datastring="";	
	$cnx=dbconn();
	$sql = 'SELECT c.idComp, c.nomComp, c.descComp, c.imgComp, c.qteStockComp, c.qteAlerteComp FROM Composant c';
	$pq=$cnx->prepare($sql);
	$pq->execute();
	while ($row = $pq->fetch(PDO::FETCH_OBJ)) {
			$datastring=$datastring."<composant>";
			$datastring=$datastring."<idcomp>".$row->idComp."</idcomp>";
			$datastring=$datastring."<nomcomp>".$row->nomComp."</nomcomp>";
			$datastring=$datastring."<desccomp>".$row->descComp."</desccomp>";
			$datastring=$datastring."<imgcomp>".$row->imgComp."</imgcomp>";
			$datastring=$datastring."<qtestockcomp>".$row->qteStockComp."</qtestockcomp>";
			$datastring=$datastring."<seuilalertecomp>".$row->qteAlerteComp."</seuilalertecomp>";
			$datastring=$datastring."</composant>";
	}
	$response=new WsResponse();
	$response->setStatusCode("1");
	$response->setStatusMessage("OK");
	$response->setData($datastring);
	echo($response->toXml());
}

function getComposantID($id){
	$datastring="";	
	$cnx=dbconn();
	$sql = 'SELECT idComp, nomComp, descComp, imgComp, qteStockComp, qteAlerteComp FROM Composant WHERE idComp = ?';
	$pq=$cnx->prepare($sql);
	$pq->bindParam(1, $id);
	$pq->execute();
	$response=new WsResponse();	
	if ($pq->rowCount()!=1){
		$response->setStatusCode("-1");
		$response->setStatusMessage("Composant introuvable");
	}else{
		$row = $pq->fetch(PDO::FETCH_OBJ);
		$datastring=$datastring."<composant>";
		$datastring=$datastring."<idcomp>".$row->idComp."</idcomp>";
		$datastring=$datastring."<nomcomp>".$row->nomComp."</nomcomp>";
		$datastring=$datastring."<desccomp>".$row->descComp."</desccomp>";
		$datastring=$datastring."<imgcomp>".$row->imgComp."</imgcomp>";
		$datastring=$datastring."<qtestockcomp>".$row->qteStockComp."</qtestockcomp>";
		$datastring=$datastring."<seuilalertecomp>".$row->qteAlerteComp."</seuilalertecomp>";
		$datastring=$datastring."</composant>";	
		$response->setStatusCode("1");
		$response->setStatusMessage("OK");
		$response->setData($datastring);
	}
	echo($response->toXml());
}


function getComposantNom($nom){
	$datastring="";	
	$cnx=dbconn();
	$sql = 'SELECT idComp, nomComp, descComp, imgComp, qteStockComp, qteAlerteComp FROM Composant WHERE nomComp LIKE :str';	
	$pq=$cnx->prepare($sql);
	//$pq->bindParam(1,$nom);	
	$pq->execute(array(':str'=>'%'.$nom.'%'));
	$response=new WsResponse();
	if ($pq->rowCount()==0){
		//echec de l'appel
		$response->setStatusCode("-1");
		$response->setStatusMessage("Composant introuvable");
	}else{
		while ($row = $pq->fetch(PDO::FETCH_OBJ)) {
			$datastring=$datastring."<composant>";
			$datastring=$datastring."<idcomp>".$row->idComp."</idcomp>";
			$datastring=$datastring."<nomcomp>".$row->nomComp."</nomcomp>";
			$datastring=$datastring."<desccomp>".$row->descComp."</desccomp>";
			$datastring=$datastring."<imgcomp>".$row->imgComp."</imgcomp>";
			$datastring=$datastring."<qtestockcomp>".$row->qteStockComp."</qtestockcomp>";
			$datastring=$datastring."<seuilalertecomp>".$row->qteAlerteComp."</seuilalertecomp>";
			$datastring=$datastring."</composant>";
		}
		//succes de l'appel		
		$response->setStatusCode("1");
		$response->setStatusMessage("OK");
		$response->setData($datastring);
	}
	echo($response->toXml());
}

//retourne la liste de tous les composants pour un produit donné
function getComposantsParProduit($idProduitFab){
	$datastring="";	
	$cnx=dbconn();
	$sql = 'SELECT idComp, nomComp, descComp, imgComp, qteStockComp, qteAlerteComp, qte FROM Composant,Servir WHERE PRODUIT_FABRIQUE_idProd=COMPOSANT_idComp AND PRODUIT_FABRIQUE_idProd = ?';
	$pq=$cnx->prepare($sql);
	$pq->bindParam(1, $idProduitFab);
	$pq->execute();
	$response=new WsResponse();	
	if ($pq->rowCount()!=1){
		$response->setStatusCode("-1");
		$response->setStatusMessage("Composant introuvable");
	}else{
		$row = $pq->fetch(PDO::FETCH_OBJ);
		$datastring=$datastring."<composant>";
		$datastring=$datastring."<idcomp>".$row->idComp."</idcomp>";
		$datastring=$datastring."<nomcomp>".$row->nomComp."</nomcomp>";
		$datastring=$datastring."<desccomp>".$row->descComp."</desccomp>";
		$datastring=$datastring."<imgcomp>".$row->imgComp."</imgcomp>";
		$datastring=$datastring."<qtestockcomp>".$row->qteStockComp."</qtestockcomp>";
		$datastring=$datastring."<seuilalertecomp>".$row->qteAlerteComp."</seuilalertecomp>";
		$datastring=$datastring."<quantite>".$row->qte."</quantite>";
		$datastring=$datastring."</composant>";	
		$response->setStatusCode("1");
		$response->setStatusMessage("OK");
		$response->setData($datastring);
	}
	echo($response->toXml());
}

function addComposant($nom, $desc, $img, $qtStock, $qtAlerte){
	$cnx=dbconn();
	$sql = 'INSERT into Composant(idComp, nomComp, descComp, imgComp, qteStockComp, qteAlerteComp) values ( null, ?,?,?,?,?)';
	$pq=$cnx->prepare($sql);
	$pq->bindParam(1, $nom);
	$pq->bindParam(2, $desc);
	$pq->bindParam(3, $img);
	$pq->bindParam(4, $qtStock);
	$pq->bindParam(5, $qtAlerte);
	$response=new WsResponse();			
	if($pq->execute()){
		//succes de l'insert	
		$response->setStatusCode("1");
		$response->setStatusMessage("OK");
	}else{
		//echec de l'insert
		$response->setStatusCode("-1");
		$response->setStatusMessage("ECHEC AJOUT");
	}
	echo($response->toXml());
}

function updateComposant($id, $nom, $desc, $img, $qtStock, $qtAlerte){
	$cnx=dbconn();
	$sql = 'UPDATE Composant SET  nomComp=? , descComp=?, imgComp=?, qteStockComp=?, qteAlerteComp=? where idComp=?';
	$pq=$cnx->prepare($sql);
	$pq->bindParam(1, $nom);
	$pq->bindParam(2, $desc);
	$pq->bindParam(3, $img);
	$pq->bindParam(4, $qtStock);
	$pq->bindParam(5, $qtAlerte);
	$pq->bindParam(6, $id);
	$response=new WsResponse();			
	if($pq->execute()){
		//succes de l'insert	
		$response->setStatusCode("1");
		$response->setStatusMessage("OK");
	}else{
		//echec de l'insert
		$response->setStatusCode("-1");
		$response->setStatusMessage("ECHEC MAJ");
	}
	echo($response->toXml());
}

function deleteComposant($id){
	$cnx=dbconn();
	$sql = 'DELETE from Composant where idComp=?';
	$pq=$cnx->prepare($sql);
	$pq->bindParam(1, $id);
	$response=new WsResponse();			
	if($pq->execute()){
		//succes de l'insert	
		$response->setStatusCode("1");
		$response->setStatusMessage("OK");
	}else{
		//echec de l'insert
		$response->setStatusCode("-1");
		$response->setStatusMessage("ECHEC SUPPRESSION");
	}
	echo($response->toXml());
}



//////////////////////////////////////// Produits Fabriques /////////////////////////////////////
function getProduitsFabriques(){
	$datastring="";	
	$cnx=dbconn();
	$sql = 'SELECT pf.idProd, pf.typeProd, pf.nomProd, pf.descProd, pf.imgProd, pf.qteStockProd, pf.qteAlerteProd, pf.prixVenteHTProd, pf.versionProd, pf.dateCreaProd FROM Produit pf';
	$pq=$cnx->prepare($sql);
	$pq->execute();
	while ($row = $pq->fetch(PDO::FETCH_OBJ)) {
			$datastring=$datastring."<produit>";
			$datastring=$datastring."<idprod>".$row->idProd."</idprod>";
			$datastring=$datastring."<typeprod>".$row->typeProd."</typeprod>";			
			$datastring=$datastring."<nomprod>".$row->nomProd."</nomprod>";
			$datastring=$datastring."<descprod>".$row->descProd."</descprod>";
			$datastring=$datastring."<imgprod>".$row->imgProd."</imgcomp>";
			$datastring=$datastring."<qtestockprod>".$row->qteStockProd."</qtestockprod>";
			$datastring=$datastring."<seuilalerteprod>".$row->qteAlerteProd."</seuilalerteprod>";
			$datastring=$datastring."<prixventehtprod>".$row->prixVenteHTProd."</prixventehtprod>";
			$datastring=$datastring."<versionprod>".$row->versionProd."</versionprod>";
			$datastring=$datastring."<datecreaprod>".$row->dateCreaProd."</datecreaprod>";
			$datastring=$datastring."</produit>";
	}
	$response=new WsResponse();
	$response->setStatusCode("1");
	$response->setStatusMessage("OK");
	$response->setData($datastring);
	echo($response->toXml());
}

function getProduitFabriqueID($id){
	$datastring="";	
	$cnx=dbconn();
	$sql = 'SELECT pf.idProd, pf.typeProd, pf.nomProd, pf.descProd, pf.imgProd, pf.qteStockProd, pf.qteAlerteProd, pf.prixVenteHTProd, pf.versionProd, pf.dateCreaProd FROM Produit pf  WHERE pf.idProd = ?';
	$pq=$cnx->prepare($sql);
	$pq->bindParam(1, $id);
	$pq->execute();
	$response=new WsResponse();	
	if ($pq->rowCount()!=1){
		$response->setStatusCode("-1");
		$response->setStatusMessage("Composant introuvable");
	}else{
		$row = $pq->fetch(PDO::FETCH_OBJ);
		$datastring=$datastring."<produit>";
		$datastring=$datastring."<idprod>".$row->idProd."</idprod>";
		$datastring=$datastring."<typeprod>".$row->typeProd."</typeprod>";		
		$datastring=$datastring."<nomprod>".$row->nomProd."</nomprod>";
		$datastring=$datastring."<descprod>".$row->descProd."</descprod>";
		$datastring=$datastring."<imgprod>".$row->imgProd."</imgcomp>";
		$datastring=$datastring."<qtestockprod>".$row->qteStockProd."</qtestockprod>";
		$datastring=$datastring."<seuilalerteprod>".$row->qteAlerteProd."</seuilalerteprod>";
		$datastring=$datastring."<prixventehtprod>".$row->prixVenteHTProd."</prixventehtprod>";
		$datastring=$datastring."<versionprod>".$row->versionProd."</versionprod>";
		$datastring=$datastring."<datecreaprod>".$row->dateCreaProd."</datecreaprod>";
		$datastring=$datastring."</produit>";
		$response->setStatusCode("1");
		$response->setStatusMessage("OK");
		$response->setData($datastring);
	}
	echo($response->toXml());
}


function getProduitFabriqueNom($nom){
	$datastring="";	
	$cnx=dbconn();
	$sql = 'SELECT pf.idProd, pf.typeProd, pf.nomProd, pf.descProd, pf.imgProd, pf.qteStockProd, pf.qteAlerteProd, pf.prixVenteHTProd, pf.versionProd, pf.dateCreaProd FROM Produit pf  WHERE pf.nomProd LIKE :str';
	$pq=$cnx->prepare($sql);
	//$pq->bindParam(1,$nom);	
	$pq->execute(array(':str'=>'%'.$nom.'%'));
	$response=new WsResponse();
	if ($pq->rowCount()==0){
		//echec de l'appel
		$response->setStatusCode("-1");
		$response->setStatusMessage("Composant introuvable");
	}else{
		while ($row = $pq->fetch(PDO::FETCH_OBJ)) {
			$datastring=$datastring."<produit>";
			$datastring=$datastring."<idprod>".$row->idProd."</idprod>";
			$datastring=$datastring."<typeprod>".$row->typeProd."</typeprod>";
			$datastring=$datastring."<nomprod>".$row->nomProd."</nomprod>";
			$datastring=$datastring."<descprod>".$row->descProd."</descprod>";
			$datastring=$datastring."<imgprod>".$row->imgProd."</imgcomp>";
			$datastring=$datastring."<qtestockprod>".$row->qteStockProd."</qtestockprod>";
			$datastring=$datastring."<seuilalerteprod>".$row->qteAlerteProd."</seuilalerteprod>";
			$datastring=$datastring."<prixventehtprod>".$row->prixVenteHTProd."</prixventehtprod>";
			$datastring=$datastring."<versionprod>".$row->versionProd."</versionprod>";
			$datastring=$datastring."<datecreaprod>".$row->dateCreaProd."</datecreaprod>";
			$datastring=$datastring."</produit>";
		}
		//succes de l'appel		
		$response->setStatusCode("1");
		$response->setStatusMessage("OK");
		$response->setData($datastring);
	}
	echo($response->toXml());
}

function addProduitFabrique($nom, $desc, $img, $qtStock, $qtAlerte, $pxVente, $version, $dateCreation){
	$cnx=dbconn();
	$sql = 'INSERT into Produit(idProd, typeProd, nomProd, descProd, imgProd, qteStockProd, qteAlerteProd, prixVenteHTProd, versionProd, dateCreaProd) values ( null,"fabrique", ?,?,?,?,?,?,?,?)';
	$pq=$cnx->prepare($sql);
	$pq->bindParam(1, $nom);
	$pq->bindParam(2, $desc);
	$pq->bindParam(3, $img);
	$pq->bindParam(4, $qtStock);
	$pq->bindParam(5, $qtAlerte);
	$pq->bindParam(6, $pxVente);
	$pq->bindParam(7, $version);
	$pq->bindParam(8, $dateCreation);
	$response=new WsResponse();			
	if($pq->execute()){
		//succes de l'insert	
		$response->setStatusCode("1");
		$response->setStatusMessage("OK");
	}else{
		//echec de l'insert
		$response->setStatusCode("-1");
		$response->setStatusMessage("ECHEC AJOUT");
	}
	echo($response->toXml());
}

function updateProduitFabrique($id, $nom, $desc, $img, $qtStock, $qtAlerte, $pxVente, $version, $dateCreation){
	$cnx=dbconn();
	$sql = 'UPDATE Produit SET typeProd="fabrique", nomProd=? , descProd=?, imgProd=?, qteStockProd=?, qteAlerteProd=?, prixventeHTProd=?, versionProd=?, dateCreaProd=? where idProd=?';
	$pq=$cnx->prepare($sql);
	$pq->bindParam(1, $nom);
	$pq->bindParam(2, $desc);
	$pq->bindParam(3, $img);
	$pq->bindParam(4, $qtStock);
	$pq->bindParam(5, $qtAlerte);
	$pq->bindParam(6, $pxVente);
	$pq->bindParam(7, $version);
	$pq->bindParam(8, $dateCreation);
	$pq->bindParam(9, $id);
	$response=new WsResponse();			
	if($pq->execute()){
		//succes de l'insert	
		$response->setStatusCode("1");
		$response->setStatusMessage("OK");
	}else{
		//echec de l'insert
		$response->setStatusCode("-1");
		$response->setStatusMessage("ECHEC MAJ");
	}
	echo($response->toXml());
}

function deleteProduitFabrique($id){
	$cnx=dbconn();
	$sql = 'DELETE from Produit where idProd=?';
	$pq=$cnx->prepare($sql);
	$pq->bindParam(1, $id);
	$response=new WsResponse();			
	if($pq->execute()){
		//succes de l'insert	
		$response->setStatusCode("1");
		$response->setStatusMessage("OK");
	}else{
		//echec de l'insert
		$response->setStatusCode("-1");
		$response->setStatusMessage("ECHEC SUPPRESSION");
	}
	echo($response->toXml());
}





//////////////////////////////////////// Stock /////////////////////////////////////


// -- vente de produits
function venteProduits($idProd, $nbUnits){
	$cnx=dbconn();
	$sql = 'UPDATE Produit SET qteStockProd = qteStockProd - ? where idProd=?';
	$pq=$cnx->prepare($sql);
	$pq->bindParam(1, $nbUnits);
	$pq->bindParam(2, $idProd);

	$response=new WsResponse();			
	if($pq->execute()){
		//succes de l'insert	
		$response->setStatusCode("1");
		$response->setStatusMessage("OK");
	}else{
		//echec de l'insert
		$response->setStatusCode("-1");
		$response->setStatusMessage("ECHEC MAJ");
	}
	echo($response->toXml());
}
//Approvisionner
function approvisionnerProduits($idProd, $nbUnits){
	$cnx=dbconn();
	$sql = 'UPDATE Produit SET qteStockProd = qteStockProd + ? where idProd=?';
	$pq=$cnx->prepare($sql);
	$pq->bindParam(1, $nbUnits);
	$pq->bindParam(2, $idProd);

	$response=new WsResponse();			
	if($pq->execute()){
		//succes de l'insert	
		$response->setStatusCode("1");
		$response->setStatusMessage("OK");
	}else{
		//echec de l'insert
		$response->setStatusCode("-1");
		$response->setStatusMessage("ECHEC MAJ");
	}
	echo($response->toXml());
}

//MAJ stock
function majStockProduits($idProd, $nbUnits){
	$cnx=dbconn();
	$sql = 'UPDATE Produit SET qteStockProd = ? where idProd=?';
	$pq=$cnx->prepare($sql);
	$pq->bindParam(1, $nbUnits);
	$pq->bindParam(2, $idProd);

	$response=new WsResponse();			
	if($pq->execute()){
		//succes de l'insert	
		$response->setStatusCode("1");
		$response->setStatusMessage("OK");
	}else{
		//echec de l'insert
		$response->setStatusCode("-1");
		$response->setStatusMessage("ECHEC MAJ");
	}
	echo($response->toXml());
}

//retourne la liste des composants dont le niveau de stock est trop bas
function composantsStockInsuffisant(){
	$datastring="";	
	$cnx=dbconn();
	$sql = 'SELECT pf.idProd, pf.typeProd, pf.nomProd, pf.descProd, pf.imgProd, pf.qteStockProd, pf.qteAlerteProd, pf.prixVenteHTProd, pf.versionProd, pf.dateCreaProd FROM Produit pf WHERE pf.qteStockProd<pf.qteAlerteProd';
	$pq=$cnx->prepare($sql);
	$pq->execute();
	while ($row = $pq->fetch(PDO::FETCH_OBJ)) {
			$datastring=$datastring."<produit>";
			$datastring=$datastring."<idprod>".$row->idProd."</idprod>";
			$datastring=$datastring."<typeprod>".$row->typeProd."</typeprod>";			
			$datastring=$datastring."<nomprod>".$row->nomProd."</nomprod>";
			$datastring=$datastring."<descprod>".$row->descProd."</descprod>";
			$datastring=$datastring."<imgprod>".$row->imgProd."</imgcomp>";
			$datastring=$datastring."<qtestockprod>".$row->qteStockProd."</qtestockprod>";
			$datastring=$datastring."<seuilalerteprod>".$row->qteAlerteProd."</seuilalerteprod>";
			$datastring=$datastring."<prixventehtprod>".$row->prixVenteHTProd."</prixventehtprod>";
			$datastring=$datastring."<versionprod>".$row->versionProd."</versionprod>";
			$datastring=$datastring."<datecreaprod>".$row->dateCreaProd."</datecreaprod>";
			$datastring=$datastring."</produit>";
	}
	$response=new WsResponse();
	$response->setStatusCode("1");
	$response->setStatusMessage("OK");
	$response->setData($datastring);
	echo($response->toXml());
}





//////////////////////////////////////// Phase /////////////////////////////////////

//ajoute une phase a la fin de la phase d’un produit.
function addPhase($id,$idProdFab, $desc, $img, $duree){
	$cnx=dbconn();
	$sql = 'INSERT into Phase(nOrdrePhase, PRODUIT_FABRIQUE_idProd, descPhase, imgPhase, dureePhase) values (?,?,?,?,?)';
	$pq=$cnx->prepare($sql);
	$pq->bindParam(1, $id);
	$pq->bindParam(2, $idProdFab);
	$pq->bindParam(3, $desc);
	$pq->bindParam(4, $img);
	$pq->bindParam(5, $duree);

	$response=new WsResponse();			
	if($pq->execute()){
		//succes de l'insert	
		$response->setStatusCode("1");
		$response->setStatusMessage("OK");
	}else{
		//echec de l'insert
		$response->setStatusCode("-1");
		$response->setStatusMessage("ECHEC AJOUT");
	}
	echo($response->toXml());
	
}

//supprime une phase identifiee par l’id de prod et le num d’ordre
function deletePhase($id,$idProdFab){
	$cnx=dbconn();
	$sql = 'DELETE from Phase where nOrdrePhase=? AND PRODUIT_FABRIQUE_idProd=?';
	$pq=$cnx->prepare($sql);
	$pq->bindParam(1, $id);
	$pq->bindParam(2, $idProdFab);	
	$response=new WsResponse();			
	if($pq->execute()){
		//succes de l'insert	
		$response->setStatusCode("1");
		$response->setStatusMessage("OK");
	}else{
		//echec de l'insert
		$response->setStatusCode("-1");
		$response->setStatusMessage("ECHEC SUPPRESSION");
	}
	echo($response->toXml());
}

//modifie une phase identifiee par l’id de prod et le num d’ordre
function updatePhase($id, $idProdFab, $desc, $img, $duree){
	$cnx=dbconn();
	$sql = 'UPDATE Phase SET descPhase=? , imgPhase=?, dureePhase=? where PRODUIT_FABRIQUE_idProd=? AND nOrdrePhase=?';
	$pq=$cnx->prepare($sql);
	$pq->bindParam(1, $desc);
	$pq->bindParam(2, $img);
	$pq->bindParam(3, $duree);
	$pq->bindParam(4, $id);
	$pq->bindParam(5, $idProdFab);
	$response=new WsResponse();			
	if($pq->execute()){
		//succes de l'insert	
		$response->setStatusCode("1");
		$response->setStatusMessage("OK");
	}else{
		//echec de l'insert
		$response->setStatusCode("-1");
		$response->setStatusMessage("ECHEC MAJ");
	}
	echo($response->toXml());
}

//retourne la liste de toutes les phases de fabrication d’un produit
function getPhasesParProduit($idProdFab){
	$datastring="";	
	$cnx=dbconn();
	$sql = 'SELECT nOrdrePhase, PRODUIT_FABRIQUE_idProd, descPhase, imgPhase, dureePhase FROM Phase pf  WHERE PRODUIT_FABRIQUE_idProd = ?';
	$pq=$cnx->prepare($sql);
	$pq->bindParam(1, $idProdFab);
	$pq->execute();
	while ($row = $pq->fetch(PDO::FETCH_OBJ)) {
		$datastring=$datastring."<phase>";
		$datastring=$datastring."<nOrdrePhase>".$row->nOrdrePhase."</nOrdrePhase>";
		$datastring=$datastring."<PRODUIT_FABRIQUE_idProd>".$row->PRODUIT_FABRIQUE_idProd."</PRODUIT_FABRIQUE_idProd>";		
		$datastring=$datastring."<descPhase>".$row->descPhase."</descPhase>";
		$datastring=$datastring."<imgPhase>".$row->imgPhase."</imgPhase>";
		$datastring=$datastring."<dureePhase>".$row->dureePhase."</dureePhase>";
		$datastring=$datastring."</phase>";
	}
	$response=new WsResponse();
	$response->setStatusCode("1");
	$response->setStatusMessage("OK");
	$response->setData($datastring);
	echo($response->toXml());
}



?>